<?php

class itrialEmail {

    const SENDER = 'itrial_cs@nmg.com.hk';

    protected $member_profiles = array();
    protected $smarty = null;

    function __construct() {
        
    }

    protected function get_member_profiles() {
        return $this->member_profiles;
    }

    protected function get_member_profile($mid) {
        if (!isset($this->member_profiles[$mid])) {
            $sql = "SELECT * FROM member_imore WHERE id={$mid}";
            $data = db_handle($sql);
            $this->member_profiles[$mid] = isset($data['result'][0]) ? $data['result'][0] : array();
        }
        return $this->member_profiles[$mid];
    }

    protected function get_smarty() {
        if ($this->smarty === null) {
            include_once 'Smarty/Smarty.class.php';
            $smarty = new Smarty();
            $smarty->left_delimiter = '{#';
            $smarty->right_delimiter = '#}';
            $smarty->compile_dir = CACHE_PATH . '/tmpl/Smarty/templates_c';
            $smarty->template_dir = VIEW_PATH . '/';
            $smarty->cache_dir = CACHE_PATH . '/tmpl/Smarty/cache';
            $this->smarty = $smarty;
        }
        return $this->smarty;
    }

    public function send($mid_or_email, $email_type, array $h_param = array()) {
        if (method_exists($this, $email_type)) {
            switch (true) {
                case is_array($mid_or_email):
                    foreach ($mid_or_email as $v) {
                        $send_status = $this->send($v, $email_type);
                    }
                    return $send_status;
                    break;
                case is_numeric($mid_or_email):
                    $member_profile = $this->get_member_profile($mid_or_email);
                    if (empty($member_profile)) {
                        $this->write_log($email_type, "send_status:0, reason:\"mid err\"");
                        return false;
                    }
                    #if ($member_profile['email_status'] != 1) {
                    #$this->write_log($email_type, "mid:{$mid_or_email}, send_status:0, reason:\"unverified email\"");
                    #return false;
                    #}
                    $receiver = $member_profile['email'];
                    $h_param['member_profile'] = $member_profile;
                    break;
                case $receiver = filter_var($mid_or_email, FILTER_VALIDATE_EMAIL):
                    break;
                default:
                    return false;
                    break;
            }

            list($subject, $content) = call_user_func(array($this, $email_type), $h_param);

            $send_status = sendMail($receiver, self::SENDER, $subject, $content);
            file_put_contents(LOG . DIRECTORY_SEPARATOR . 'email' . DIRECTORY_SEPARATOR . 'lastoutput.html', $content);
            $this->write_log($email_type, "mid:{$mid_or_email}, receiver:{$receiver}, subject:\"{$subject}\", send_status:{$send_status}");

            return $send_status;
        }

        return false;
    }

    protected function write_log($email_type, $content) {
        $content = date("Y-m-d H:i:s", time()) . " | " . $content . "\r\n";
        file_put_contents(LOG . DIRECTORY_SEPARATOR . 'email' . DIRECTORY_SEPARATOR . $email_type . '.log', $content, FILE_APPEND);
    }

    //A推介新產品(每星期一次，所有用戶)
    protected function introducing_new_products(array $h_param = array()) {
        $subject = 'iTrial by iMORE 今周最新試用邀請：xxxxxxxxxxxxx';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/introducing_new_products.php');

        return array($subject, $content);
    }

    //B成為會員 (沒有同時申請成為試用專員及體驗達人)
    protected function become_member(array $h_param = array()) {
        $subject = '你已成功登記成為 iMORE.hk 會員，請按入激活賬號';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/become_member.php');

        return array($subject, $content);
    }

    //C申請成為試用專員成功 (成為會員後沒有同時申請成為體驗達人)
    protected function become_commissioner(array $h_param = array()) {
        $subject = 'iMORE.hk 會員已成功申請成為試用專員之確認信 (尚未申請成為體驗達人)';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/become_commissioner.php');

        return array($subject, $content);
    }

    //D. 申請成為體驗達人 (一次完成所有申請步驟)
    protected function app_experience_daren(array $h_param = array()) {
        $subject = 'iMORE.hk 會員已一次性完成申請成為 (會員、試用達人及體驗達人) 之通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/app_experience_daren.php');

        return array($subject, $content);
    }

    //E1. 申請成為體驗達人成功 (通過後台審核)
    protected function verify_experience_daren(array $h_param = array()) {
        $subject = '通過審核成為iTrial by iMORE 體驗達人之確認信';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/verify_experience_daren.php');

        return array($subject, $content);
    }

    //E2. 未能成為體驗達人 (通過後台審核)
    protected function reject_experience_daren(array $h_param = array()) {
        $subject = '未能成功通過審核成為 iTrial by iMORE體驗達人之通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/reject_experience_daren.php');

        return array($subject, $content);
    }

    //F. 申請產品試用 / 產品體驗 / 活動 (會員確認提交申請時)
    protected function app_try_fa(array $h_param = array()) {
        $subject = '申請iTrial by iMORE (產品名:  ' . $h_param['cname'] . ') 產品體驗通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/app_try_fa.php');

        return array($subject, $content);
    }

    protected function app_try_fb(array $h_param = array()) {
        $subject = '申請iTrial by iMORE (產品名:  ' . $h_param['cname'] . ') 產品試用通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/app_try_fb.php');

        return array($subject, $content);
    }

    protected function app_try_fc(array $h_param = array()) {
        $subject = '申請iTrial by iMORE (活動名:  ' . $h_param['cname'] . ') :  活動通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/app_try_fc.php');

        return array($subject, $content);
    }

    //G. 獲邀產品試用 / 產品體驗 / 活動 (通過後台審核)
    protected function invited_to_try_p(array $h_param = array()) {
        $subject = '恭喜您已獲邀參加 iTrial by iMORE  的產品' . $h_param['cname'] . ') 產品試用通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
             'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/invited_to_try_p.php');
        

        return array($subject, $content);
    }
    
    protected function invited_to_try_e(array $h_param = array()) {
        $subject = '恭喜您已獲邀參加 iTrial by iMORE 的 '.$h_param['event_type'].' (活動名' . $h_param['cname'] . ')';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
             'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/invited_to_try_e.php');
        

        return array($subject, $content);
    }

    //H. 提醒提交報告 (獲邀會員，需要提交報告，到期前3日未交)
    protected function remind_submit_report(array $h_param = array()) {
        $subject = 'iTrial by iMORE (產品名: XXXXX) 產品試用報告重要提示';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/remind_submit_report.php');

        return array($subject, $content);
    }

    //I. 提交報告成功 (用戶提交報告時)
    protected function submit_report_success(array $h_param = array()) {
        $subject = 'iTrial by iMORE (產品名:  ' . $h_param['cname'] . ') 產品試用報告已成功提交通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/submit_report_success.php');

        return array($subject, $content);
    }

    //J. 逾期提交報告 (獲邀會員，需要提交報告，已過期)
    protected function overdue_submit_report(array $h_param = array()) {
        $subject = 'iTrial by iMORE (產品名: ' . $h_param['cname'] . ') 產品試用已逾期提交報告通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/overdue_submit_report.php');

        return array($subject, $content);
    }

    //K. 申請延期成功 (獲邀會員，需要提交報告，已過期，但申請逾期)
    protected function app_overdue_success(array $h_param = array()) {
        $subject = 'iTrial by iMORE (產品名: ' . $h_param['cname'] . ') 產品試用報告已逾期提交成功';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/app_overdue_success.php');

        return array($subject, $content);
    }

    //L. 試用報告被駁回
    protected function app_overdue_reject(array $h_param = array()) {
        $subject = 'iTrial by iMORE (產品名: XXXXX) 產品試用報告被駁回通知';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile']
        ));

        $content = $smarty->fetch('email/app_overdue_reject.php');

        return array($subject, $content);
    }
    
    protected function forgetPassword(array $h_param = array()) {
        $subject = '設定新的 iTrial by iMORE 帳戶密碼';

        $smarty = $this->get_smarty();
        $smarty->assign(array(
            'member_profile' => $h_param['member_profile'],
            'h_param' => $h_param,
        ));

        $content = $smarty->fetch('email/forgetPassword.php');

        return array($subject, $content);
    }
    
    protected function common_email(array $h_param = array()) {
        $subject = $h_param['title'];

        $content=$h_param['content'];

        return array($subject, $content);
    }

}

?>
